package com.yisoo.rs.rsfile.controller;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Base64;
import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;


import com.yisoo.rs.common.utils.RRException;
import com.yisoo.rs.rsfile.feign.MoiveFeignService;
import com.yisoo.rs.rsfile.pojo.FileProperties;
import com.yisoo.rs.rsfile.service.impl.MyFileService;
import com.yisoo.rs.rsfile.utils.ConversionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.yisoo.rs.rsfile.entity.BakEntity;
import com.yisoo.rs.rsfile.service.BakService;
import com.yisoo.rs.common.utils.PageUtils;
import com.yisoo.rs.common.utils.R;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.xml.crypto.Data;


/**
 * 
 *
 * @author mmciel
 * @email 761998179@qq.com
 * @date 2021-11-30 15:15:27
 */
@RestController
@RequestMapping("rsfile/bak")
public class BakController {
    @Autowired
    private BakService bakService;

    @Autowired
    private MyFileService myFileService;

    @Autowired
    private MoiveFeignService moiveFeignService;

    @RequestMapping("/test")
    public R test(){
        R msg = moiveFeignService.test();
        System.out.println("msgmsg");
        return R.ok(msg);
    }

    /**
     * 导入
     */
    @RequestMapping("/import/{fileId}")
    public R importById(@PathVariable("fileId")Long fileId){
//        System.out.println(fileId);
        boolean result = bakService.importFileById(fileId);
        if(result){
            BakEntity bakEntity = bakService.getById(fileId);
            bakEntity.setFileImport("导入成功");
            bakService.updateById(bakEntity);
            return R.ok();
        }else{
            return R.error("导入失败");
        }
    }



//    @GetMapping("/parse/{fileId}")
//    public R parseFile(@PathVariable("fileId")Long fileId){
//        BakEntity bakEntity = bakService.getById(fileId);
//
//        byte[] content  = bakEntity.getFileContent();
//        String result = new String(content);
//
//        return R.ok().put("content","s");
//    }

    @PostMapping("/upload")
    public R uploadFile(@RequestParam("file") MultipartFile file){
//        获取文件名
        String time = String.valueOf(System.currentTimeMillis());
        String fileName = time + "-" +StringUtils.cleanPath(file.getOriginalFilename());
//        写入文件到磁盘，并返回绝对路径
        String location = myFileService.storeFile(fileName,file);
        byte[] data = null;
        try {

            String result = new BufferedReader(new InputStreamReader(file.getInputStream()))
                    .lines().collect(Collectors.joining(System.lineSeparator()));
            System.out.println(result);
            data = result.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        存到数据库
        BakEntity bakEntity = new BakEntity(
                null,
                fileName,
                new Date(),
                data,
                "未导入",
                location
        );
        bakService.save(bakEntity);
        System.out.println(fileName);
        return R.ok();
    }


    @PostMapping("/upload/more")
    public R uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
//        暂不处理
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bakService.queryPageDelContent(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{fileId}")
    public R info(@PathVariable("fileId") Long fileId){
		BakEntity bak = bakService.getByIdDelContent(fileId);

        return R.ok().put("bak", bak);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BakEntity bak){
		bakService.save(bak);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BakEntity bak){
		bakService.updateById(bak);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] fileIds){
		bakService.removeByIds(Arrays.asList(fileIds));

        return R.ok();
    }

}
